package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Sharestat;
import com.qianniu.napi.admin.service.ISharestatService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 分享统计
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-12
 */
@RestController
public class SharestatController {

    private static final Logger logger = LoggerFactory.getLogger(SharestatController.class);

    @Autowired
    private ISharestatService sharestatService;

    @RequestMapping("/api/sharestat/list")
    @ResponseBody
    public JsonDO list(@RequestParam(value = "status", required = false) String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       HttpServletRequest req) {
        JsonDO json = new JsonDO();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = sharestatService.selectPage(page, new EntityWrapper<Sharestat>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        json.setData(page.getRecords());
        return json;
    }

    @RequestMapping("/api/sharestat/add")
    @ResponseBody
    public JsonDO sharestatAdd(@Valid Sharestat sharestat,
                               BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(400);
            return json;
        }
        sharestat.setCreatetime(new Date());
        sharestat.insert();
        return json;
    }

    @RequestMapping("/api/sharestat/modify")
    @ResponseBody
    public JsonDO sharestatUpdate(HttpServletRequest req,
                                  Sharestat sharestat) {
        JsonDO json = new JsonDO();
//        sharestat.setModifytime(new Date());
        sharestat.updateById();
        return json;
    }

    @RequestMapping("/api/sharestat/detail")
    @ResponseBody
    public JsonDO getSharestatDetailById(@RequestParam("token") String token,
                                         @RequestParam("id") long id, HttpServletRequest req
    ) {
        JsonDO json = new JsonDO();
        Sharestat sharestat = sharestatService.selectById(id);
        json.setData(sharestat);
        return json;
    }

}
